package ynu.edu.achitectureclass.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.edu.achitectureclass.entity.Device;
import ynu.edu.achitectureclass.repository.DeviceRepository;

import java.util.List;

@Service
public class DeviceService {

    @Autowired
    private DeviceRepository deviceRepository;

    public String getCameraDeviceSerial() {
        Device cameraDevice = deviceRepository.findByType("camera");
        if (cameraDevice != null) {
            return cameraDevice.getSerialNumber();
        }
        return null;
    }

    public List<Device> getAllDevices() {
        return deviceRepository.findAll();
    }

    public Device getDeviceById(Long id) {
        return deviceRepository.findById(id).orElse(null);
    }

    public Device saveDevice(Device device) {
        return deviceRepository.save(device);
    }

    public void deleteDevice(Long id) {
        deviceRepository.deleteById(id);
    }
}